package marsrovers;

import java.util.ArrayList;
import java.util.List;

public class MarsRoversMain {

	public static void main(String[] args) {
		List<String> lines = toLines(args);
		for (String line : lines) {
			System.out.println(line);
		}
		
		Plateau plateau = getPlateau(lines);
		System.out.println("Plateau Coords: " + plateau.toString());
		
		List<Rover> rovers = getRovers(lines);
		for (Rover rover : rovers) {
			System.out.println("\n\nRover " + getIndex(rovers, rover) + "\n"
					+ "InitialPosition: " + rover.getPosition().toString());
			rover.computeFinalPosition();
			System.out.println("FinalPosition: " + rover.getPosition());
		}
	}

	private static int getIndex(List<Rover> rovers, Rover rover) {
		return rovers.indexOf(rover) + 1;
	}

	private static List<Rover> getRovers(List<String> lines) {
		// Get rover positions and coordinates from list of lines
		List<String> roversPositionsAndInstructions = lines.subList(1, lines.size());
		List<Rover> roversWithInstructionsFed = getRoversAndInstructionsFed(roversPositionsAndInstructions);
		System.out.println("Rovers and Instructions: \n");
		for (Rover rover : roversWithInstructionsFed) {
			System.out.println("\nRover " + getIndex(roversWithInstructionsFed, rover) + ": \n" 
					+ rover.toString());
		}
		
		return roversWithInstructionsFed;
	}

	private static List<Rover> getRoversAndInstructionsFed(
			List<String> roversPositionsAndInstructions) {
		List<Rover> rovers = new ArrayList<Rover>();
		
		int index = 0;
		while (index < roversPositionsAndInstructions.size()) {
			String roverPositionString = roversPositionsAndInstructions.get(index++);
			RoverPosition roverPosition = RoverPosition.toRoverPosition(roverPositionString);
			
			String instructionListAsString = roversPositionsAndInstructions.get(index++);
			List<Instruction> instructionList = getInstructionList(instructionListAsString);
			
			Rover rover = new Rover(roverPosition, instructionList);
			rovers.add(rover);
		}
		
		return rovers;
	}

	private static List<Instruction> getInstructionList(
			String instructionListAsString) {
		List<Instruction> instructionList = new ArrayList<Instruction>();
		
		for (char ch : instructionListAsString.toCharArray()) {
			instructionList.add(Instruction.toInstruction(String.valueOf(ch)));
		}
		return instructionList;
	}

	private static Plateau getPlateau(List<String> lines) {
		// Get Plateau coordinates from list of lines
		String plateauCoords = lines.get(0);
		Plateau plateau = new Plateau(
				plateauCoords.substring(0, 1), 
				plateauCoords.substring(2, plateauCoords.length()));
		return plateau;
	}
	
	// TODO(seshumadhav): Add logic for invalid input
	private static List<String> toLines(String[] args) {
		List<String> lines = new ArrayList<String>();
		int argsIndex = 0;
		int lastLineIndex = 0;
		

		// Plateau Coordinates
		String plateauCoordinates = args[argsIndex++] + " " + args[argsIndex++];
		lines.add(lastLineIndex++, plateauCoordinates);
		
		// Rover position & Instructions
		while (argsIndex < args.length) {
			String roverPosition = args[argsIndex++] + " " + args[argsIndex++]
					+ " " + args[argsIndex++];
			String roverInstructions = args[argsIndex++];
			
			lines.add(lastLineIndex++, roverPosition);
			lines.add(lastLineIndex++, roverInstructions);
		}
		
		return lines;
	}

}
